#include "selectdevice.h"
#include "ui_selectdevice.h"
#include <QDebug>
#include <QThread>

SelectDevice::SelectDevice(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SelectDevice)
{
    ui->setupUi(this);

//    ui->listWidget->addItem(new QListWidgetItem(QObject::tr("1号电池模组")));
//    ui->listWidget->addItem(new QListWidgetItem(QObject::tr("2号电池模组")));
//    ui->listWidget->addItem(new QListWidgetItem(QObject::tr("3号电池模组")));
//    ui->listWidget->addItem(new QListWidgetItem(QObject::tr("4号电池模组")));
//    ui->listWidget->addItem(new QListWidgetItem(QObject::tr("5号电池模组")));
//    ui->listWidget->addItem(new QListWidgetItem(QObject::tr("6号电池模组")));

    ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
    ui->lcdNumber_2->setSegmentStyle(QLCDNumber::Flat);
    ui->progressBar->setValue(0);
    ui->progressBar->setRange(0, 100);

}

SelectDevice::~SelectDevice()
{
    delete ui;
}

void SelectDevice::com_rec(QString data_src)
{
    if(data_src.indexOf("3,CAN,n=8") != -1)
    {
        QStringList cmd = data_src.split(",");
        QString canID = cmd.at(3);
        canID = canID.split("=").at(1);
        if(canID == "12340001")
        {
            QString orgData = cmd.at(4);
            orgData = orgData.mid(4, 16);

            batNum = orgData.mid(2, 2).toInt(&ok, 16);

            qDebug() << "CAN ID:" << canID;
            qDebug() << "data:" << orgData;
            creatListItem();
        }else if(canID == "13005000")
        {
            QString orgData = cmd.at(4);
            orgData = orgData.mid(4, 16);

            QByteArray ba;
            QDataStream packet(&ba, QIODevice::ReadWrite);
            packet.setByteOrder(QDataStream::BigEndian);
            for(int i=0; i < orgData.length(); i=i+2)
            {
                quint8 temp = orgData.mid(i, 2).toInt(&ok, 16);
                packet << temp;
            }

            quint32 vo = 0;
            qint16 cu = 0;
            quint16 pr = 0;

            packet.device()->seek(0);
            packet >> vo >> cu >> pr;

            int lcdVo = vo;
            ui->lcdNumber->display(lcdVo);
            ui->lcdNumber_2->display(cu);
            ui->progressBar->setValue(pr);

            //        ui->lineEdit_1->setText(QString("%1 mV").arg(vo));
            //        ui->lineEdit_2->setText(QString("%1 mA").arg(cu));
            //        ui->lineEdit_3->setText(QString("%1 %").arg(pr));
        }
    }
}

void SelectDevice::info_set_send(QByteArray ba)
{
    emit SendSet(ba);
}

void SelectDevice::on_pushButton_clicked()
{
    emit openDevice(ui->listWidget->currentIndex().row());
}

void SelectDevice::creatListItem()
{
    if(batNum != ui->listWidget->count())
    {
        ui->listWidget->clear();
        for(int i=1; i <= batNum; i++)
        {
            ui->listWidget->addItem(new QListWidgetItem(QString("%1号电池模组").arg(i)));
        }
        //        ui->listWidget->addItem(new QListWidgetItem(QObject::tr("1号电池组")));
        //        ui->listWidget->addItem(new QListWidgetItem(QObject::tr("2号电池组")));
        //        ui->listWidget->addItem(new QListWidgetItem(QObject::tr("3号电池组")));
        //        ui->listWidget->addItem(new QListWidgetItem(QObject::tr("4号电池组")));
        //        ui->listWidget->addItem(new QListWidgetItem(QObject::tr("5号电池组")));
        //        ui->listWidget->addItem(new QListWidgetItem(QObject::tr("6号电池组")));
    }
}

void SelectDevice::on_pushButton_2_clicked()
{
    if(1)
    {
        QByteArray sendBa;
        QByteArray sendID = "@3,CAN,n=8:ID=13002000,";
        sendBa.append(sendID);

        quint16 temp[4];
        temp[0] = ui->lineEdit_6->text().toUInt();
        temp[1] = ui->lineEdit_8->text().toUInt();
        temp[2] = ui->lineEdit_7->text().toUInt();
        temp[3] = ui->lineEdit_5->text().toUInt();

        QByteArray tempBa;
        QDataStream packets(&tempBa, QIODevice::ReadWrite);
        packets.setByteOrder(QDataStream::BigEndian);

        packets << temp[0] << temp[1] << temp[2] << temp[3];

        quint8 tp[8];
        packets.device()->seek(0);
        packets >> tp[0] >> tp[1] >> tp[2] >> tp[3] >> tp[4] >> tp[5] >> tp[6] >> tp[7];
        tempBa.clear();
        for(int i=0; i < 8; i++)
        {
            tempBa.append(ToStr(tp[i], 16, 2));
        }
        sendBa.append(tempBa);
        sendBa.append("#");

        emit SendSet(sendBa);
    }

    if(1)
    {
        QByteArray sendBa;
        QByteArray sendID = "@3,CAN,n=8:ID=13002100,";
        sendBa.append(sendID);

        quint16 temp[4];
        temp[0] = ui->lineEdit_9->text().toUInt();
        temp[1] = ui->lineEdit_10->text().toUInt();
        temp[2] = ui->lineEdit_11->text().toUInt();
        temp[3] = ui->lineEdit_14->text().toUInt();

        QByteArray tempBa;
        QDataStream packets(&tempBa, QIODevice::ReadWrite);
        packets.setByteOrder(QDataStream::BigEndian);

        packets << temp[0] << temp[1] << temp[2] << temp[3];

        quint8 tp[8];
        packets.device()->seek(0);
        packets >> tp[0] >> tp[1] >> tp[2] >> tp[3] >> tp[4] >> tp[5] >> tp[6] >> tp[7];
        tempBa.clear();
        for(int i=0; i < 8; i++)
        {
            tempBa.append(ToStr(tp[i], 16, 2));
        }
        sendBa.append(tempBa);
        sendBa.append("#");

        emit SendSet(sendBa);
    }

    if(1)
    {
        QByteArray sendBa;
        QByteArray sendID = "@3,CAN,n=8:ID=13006000,";
        sendBa.append(sendID);

        quint16 temp[2];
        temp[0] = ui->lineEdit_13->text().toUInt();
        temp[1] = ui->lineEdit_15->text().toUInt();

        QByteArray tempBa;
        QDataStream packets(&tempBa, QIODevice::ReadWrite);
        packets.setByteOrder(QDataStream::BigEndian);

        packets << temp[0] << temp[1];

        quint8 tp[8];
        packets.device()->seek(0);
        packets >> tp[0] >> tp[1] >> tp[2] >> tp[3];
        tempBa.clear();
        for(int i=0; i < 4; i++)
        {
            tempBa.append(ToStr(tp[i], 16, 2));
        }
        sendBa.append(tempBa);
        sendBa.append("#");

        emit SendSet(sendBa);
    }
}


QString SelectDevice::ToStr(int number, int base, int size)
{
    return QString("%1").arg(number, size, base, QChar('0'));
}

void SelectDevice::on_pushButton_4_clicked()
{
    emit openCali();
}
